package com.jiyi.activity.datainput.sm;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jiyi.activity.datainput.DIBaseMorePicActivity;
import com.jiyi.bean.SingleImageBean;
import com.jiyi.bean.datainput.new_input.BaseCheck;
import com.jiyi.bean.datainput.new_input.SpecialFireBean;
import com.jiyi.exception.SampleProgressObserver;
import com.jiyi.inteface.DownloadPicOverInterface;
import com.jiyi.retrofit.HttpMethods;
import com.jiyi.widget.NoScrollGridView;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.base.AppMediaType;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MultipartBody;

/**
 * Created by Administrator on 2018/3/22.
 */

public class SpecialFirePreventionActivity extends DIBaseMorePicActivity implements DownloadPicOverInterface {
    /* 头部 */
    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;
    @BindView(R.id.rbFirePerformanceYes)
    RadioButton rbFirePerformanceYes;
    @BindView(R.id.rbFirePerformanceNo)
    RadioButton rbFirePerformanceNo;
    @BindView(R.id.rgFirePerformanceIsOK)
    RadioGroup rgFirePerformanceIsOK;
    @BindView(R.id.etFirePerformanceProblem)
    EditText etFirePerformanceProblem;
    @BindView(R.id.etFirePerformanceMeasure)
    EditText etFirePerformanceMeasure;
    @BindView(R.id.rbCableWellYes)
    RadioButton rbCableWellYes;
    @BindView(R.id.rbCableWellNo)
    RadioButton rbCableWellNo;
    @BindView(R.id.rgCableWellIsOK)
    RadioGroup rgCableWellIsOK;
    @BindView(R.id.etCableWellProblem)
    EditText etCableWellProblem;
    @BindView(R.id.etCableWellMeasure)
    EditText etCableWellMeasure;
    @BindView(R.id.rbFireWallLimitYes)
    RadioButton rbFireWallLimitYes;
    @BindView(R.id.rbFireWallLimitNo)
    RadioButton rbFireWallLimitNo;
    @BindView(R.id.rgFireWallLimitIsOK)
    RadioGroup rgFireWallLimitIsOK;
    @BindView(R.id.etFireWallLimitProblem)
    EditText etFireWallLimitProblem;
    @BindView(R.id.etFireWallLimitMeasure)
    EditText etFireWallLimitMeasure;
    @BindView(R.id.rbFireBlockYes)
    RadioButton rbFireBlockYes;
    @BindView(R.id.rbFireBlockNo)
    RadioButton rbFireBlockNo;
    @BindView(R.id.rgFireBlockIsOK)
    RadioGroup rgFireBlockIsOK;
    @BindView(R.id.etFireBlockProblem)
    EditText etFireBlockProblem;
    @BindView(R.id.etFireBlockMeasure)
    EditText etFireBlockMeasure;
    @BindView(R.id.rbTubeWellsUseYes)
    RadioButton rbTubeWellsUseYes;
    @BindView(R.id.rbTubeWellsUseNo)
    RadioButton rbTubeWellsUseNo;
    @BindView(R.id.rgTubeWellsUse)
    RadioGroup rgTubeWellsUse;
    @BindView(R.id.etTubeWellsUseProblem)
    EditText etTubeWellsUseProblem;
    @BindView(R.id.etTubeWellsUseMeasure)
    EditText etTubeWellsUseMeasure;
    @BindView(R.id.rbElectricLineYes)
    RadioButton rbElectricLineYes;
    @BindView(R.id.rbElectricLineNo)
    RadioButton rbElectricLineNo;
    @BindView(R.id.rgElectricLineIsOk)
    RadioGroup rgElectricLineIsOk;
    @BindView(R.id.etElectricLineProblem)
    EditText etElectricLineProblem;
    @BindView(R.id.etElectricLineMeasure)
    EditText etElectricLineMeasure;
    @BindView(R.id.rbElectricityLoadYes)
    RadioButton rbElectricityLoadYes;
    @BindView(R.id.rbElectricityLoadNo)
    RadioButton rbElectricityLoadNo;
    @BindView(R.id.rgElectricityLoadIsOk)
    RadioGroup rgElectricityLoadIsOk;
    @BindView(R.id.etElectricityLoadProblem)
    EditText etElectricityLoadProblem;
    @BindView(R.id.etElectricityLoadMeasure)
    EditText etElectricityLoadMeasure;
    @BindView(R.id.rbGasYes)
    RadioButton rbGasYes;
    @BindView(R.id.rbGasNo)
    RadioButton rbGasNo;
    @BindView(R.id.rgGasIsOk)
    RadioGroup rgGasIsOk;
    @BindView(R.id.etGasProblem)
    EditText etGasProblem;
    @BindView(R.id.etGasMeasure)
    EditText etGasMeasure;

    int mFirePerformanceIsOK, mCableWellIsOK, mFireWallLimitIsOK, mFireBlockIsOK, mTubeWellsUse, mElectricLineIsOk, mElectricityLoadIsOk, mGasIsOk;
    String build_id;


    @BindView(R.id.gvFirePerformance)
    NoScrollGridView gvFirePerformance;
    @BindView(R.id.gvCableWell)
    NoScrollGridView gvCableWell;
    @BindView(R.id.gvFireWallLimit)
    NoScrollGridView gvFireWallLimit;
    @BindView(R.id.gvFireBlock)
    NoScrollGridView gvFireBlock;
    @BindView(R.id.gvTubeWellsUse)
    NoScrollGridView gvTubeWellsUse;
    @BindView(R.id.gvElectricLine)
    NoScrollGridView gvElectricLine;
    @BindView(R.id.gvElectricityLoad)
    NoScrollGridView gvElectricityLoad;
    @BindView(R.id.gvGas)
    NoScrollGridView gvGas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_special_fire_prevention);
        ButterKnife.bind(this);
        init();
    }

    public void init() {
        getIntentData();
        setTop();
        setViewStatus();
        getData();
        super.setDownloadPicOverInterface(this);
    }

    private void getIntentData() {
        build_id = getIntent().getStringExtra("build_no");
        if (TextUtils.isEmpty(build_id))
            build_id = "";
    }

    private void setTop() {
        tvTitle.setText("专项防火情况");
        tvSearch.setCompoundDrawables(null, null, null, null);
        tvSearch.setText("保存");
    }

    public void setViewStatus() {
        rgFirePerformanceIsOK.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbFirePerformanceYes) {
                    mFirePerformanceIsOK = 1;
                } else {
                    mFirePerformanceIsOK = 0;
                }
            }
        });

        rgCableWellIsOK.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbCableWellYes) {
                    mCableWellIsOK = 1;
                } else {
                    mCableWellIsOK = 0;
                }
            }
        });

        rgFireWallLimitIsOK.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbFireWallLimitYes) {
                    mFireWallLimitIsOK = 1;
                } else {
                    mFireWallLimitIsOK = 0;
                }
            }
        });

        rgFireBlockIsOK.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbFireBlockYes) {
                    mFireBlockIsOK = 1;
                } else {
                    mFireBlockIsOK = 0;
                }
            }
        });

        rgTubeWellsUse.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbTubeWellsUseYes) {
                    mTubeWellsUse = 1;
                } else {
                    mTubeWellsUse = 0;
                }
            }
        });

        rgElectricLineIsOk.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbElectricLineYes) {
                    mElectricLineIsOk = 1;
                } else {
                    mElectricLineIsOk = 0;
                }
            }
        });

        rgElectricityLoadIsOk.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbElectricityLoadYes) {
                    mElectricityLoadIsOk = 1;
                } else {
                    mElectricityLoadIsOk = 0;
                }
            }
        });

        rgGasIsOk.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbGasYes) {
                    mGasIsOk = 1;
                } else {
                    mGasIsOk = 0;
                }
            }
        });
    }


    public void getData() {
        HttpMethods.getInstance().getSpecialFireInfo(UserInfoUtil.getCurrentUserAccessToken(), build_id, 2)
                .subscribe(new SampleProgressObserver<SpecialFireBean>(this) {
                    @Override
                    public void onNext(SpecialFireBean bean) {
                        if (bean == null || "".equals(bean.getGson_null())) {
                            bean = new SpecialFireBean();
                            BaseCheck bc1 = new BaseCheck();
                            bc1.setIs_ok(0);
                            bc1.setProblem("");
                            bc1.setMeasures("");
                            bean.setWall_fireproof_performance(bc1);
                            BaseCheck bc2 = new BaseCheck();
                            bc2.setIs_ok(0);
                            bc2.setProblem("");
                            bc2.setMeasures("");
                            bean.setCable_pipe_independent(bc2);
                            BaseCheck bc3 = new BaseCheck();
                            bc3.setIs_ok(0);
                            bc3.setProblem("");
                            bc3.setMeasures("");
                            bean.setWell_pipe_reach(bc3);
                            BaseCheck bc4 = new BaseCheck();
                            bc4.setIs_ok(0);
                            bc4.setProblem("");
                            bc4.setMeasures("");
                            bean.setCable_pipe_sealed(bc4);
                            BaseCheck bc5 = new BaseCheck();
                            bc5.setIs_ok(0);
                            bc5.setProblem("");
                            bc5.setMeasures("");
                            bean.setPipe_well_occupation(bc5);
                            BaseCheck bc6 = new BaseCheck();
                            bc6.setIs_ok(0);
                            bc6.setProblem("");
                            bc6.setMeasures("");
                            bean.setElectrical_wiring_equipment(bc6);
                            BaseCheck bc7 = new BaseCheck();
                            bc7.setIs_ok(0);
                            bc7.setProblem("");
                            bc7.setMeasures("");
                            bean.setFire_power_load(bc7);
                            BaseCheck bc8 = new BaseCheck();
                            bc8.setIs_ok(0);
                            bc8.setProblem("");
                            bc8.setMeasures("");
                            bean.setGas_site_standards(bc8);
                            bean.setNo_edit(1);
                        }

                        fillData(bean);
                    }
                });
    }

    private void fillData(SpecialFireBean bean) {
        if (bean.getWall_fireproof_performance().getIs_ok() == 1) {
            rbFirePerformanceYes.setChecked(true);
        }

        etFirePerformanceProblem.setText(bean.getWall_fireproof_performance().getProblem());
        etFirePerformanceMeasure.setText(bean.getWall_fireproof_performance().getMeasures());

        if (bean.getCable_pipe_independent().getIs_ok() == 1) {
            rbCableWellYes.setChecked(true);
        }

        etCableWellProblem.setText(bean.getCable_pipe_independent().getProblem());
        etCableWellMeasure.setText(bean.getCable_pipe_independent().getMeasures());

        if (bean.getWell_pipe_reach().getIs_ok() == 1) {
            rbFireWallLimitYes.setChecked(true);
        }

        etFireWallLimitProblem.setText(bean.getWell_pipe_reach().getProblem());
        etFireWallLimitMeasure.setText(bean.getWell_pipe_reach().getMeasures());

        if (bean.getCable_pipe_sealed().getIs_ok() == 1) {
            rbFireBlockYes.setChecked(true);
        }

        etFireBlockProblem.setText(bean.getCable_pipe_sealed().getProblem());
        etFireBlockMeasure.setText(bean.getCable_pipe_sealed().getMeasures());

        if (bean.getPipe_well_occupation().getIs_ok() == 1) {
            rbTubeWellsUseYes.setChecked(true);
        }

        etTubeWellsUseProblem.setText(bean.getPipe_well_occupation().getProblem());
        etTubeWellsUseMeasure.setText(bean.getPipe_well_occupation().getMeasures());

        if (bean.getElectrical_wiring_equipment().getIs_ok() == 1) {
            rbElectricLineYes.setChecked(true);
        }

        etElectricLineProblem.setText(bean.getElectrical_wiring_equipment().getProblem());
        etElectricLineMeasure.setText(bean.getElectrical_wiring_equipment().getMeasures());

        if (bean.getFire_power_load().getIs_ok() == 1) {
            rbElectricityLoadYes.setChecked(true);
        }

        etElectricityLoadProblem.setText(bean.getFire_power_load().getProblem());
        etElectricityLoadMeasure.setText(bean.getFire_power_load().getMeasures());

        if (bean.getGas_site_standards().getIs_ok() == 1) {
            rbGasYes.setChecked(true);
        }

        etGasProblem.setText(bean.getGas_site_standards().getProblem());
        etGasMeasure.setText(bean.getGas_site_standards().getMeasures());
        if (bean.getNo_edit() == 1) {
            tvSearch.setVisibility(View.VISIBLE);
        } else {
            tvSearch.setVisibility(View.INVISIBLE);
        }

        initGridViewList(bean);
    }

    void initGridViewList(SpecialFireBean specialFireBean) {
        List<SingleImageBean> singleImageBeans = new ArrayList<SingleImageBean>();

        SingleImageBean s1 = new SingleImageBean();
        s1.setPosition(0);
        s1.setDelModel(false);
        s1.setGridView(gvFirePerformance);
        s1.setImageList(specialFireBean.getWall_fireproof_performance().getImage());
        singleImageBeans.add(s1);

        SingleImageBean s2 = new SingleImageBean();
        s2.setPosition(1);
        s2.setDelModel(false);
        s2.setGridView(gvCableWell);
        s2.setImageList(specialFireBean.getCable_pipe_independent().getImage());
        singleImageBeans.add(s2);

        SingleImageBean s3 = new SingleImageBean();
        s3.setPosition(2);
        s3.setDelModel(false);
        s3.setGridView(gvFireWallLimit);
        s3.setImageList(specialFireBean.getWell_pipe_reach().getImage());
        singleImageBeans.add(s3);

        SingleImageBean s4 = new SingleImageBean();
        s4.setPosition(3);
        s4.setDelModel(false);
        s4.setGridView(gvFireBlock);
        s4.setImageList(specialFireBean.getCable_pipe_sealed().getImage());
        singleImageBeans.add(s4);

        SingleImageBean s5 = new SingleImageBean();
        s5.setPosition(4);
        s5.setDelModel(false);
        s5.setGridView(gvTubeWellsUse);
        s5.setImageList(specialFireBean.getPipe_well_occupation().getImage());
        singleImageBeans.add(s5);

        SingleImageBean s6 = new SingleImageBean();
        s6.setPosition(5);
        s6.setDelModel(false);
        s6.setGridView(gvElectricLine);
        s6.setImageList(specialFireBean.getElectrical_wiring_equipment().getImage());
        singleImageBeans.add(s6);

        SingleImageBean s7 = new SingleImageBean();
        s7.setPosition(6);
        s7.setDelModel(false);
        s7.setGridView(gvElectricityLoad);
        s7.setImageList(specialFireBean.getFire_power_load().getImage());
        singleImageBeans.add(s7);

        SingleImageBean s8 = new SingleImageBean();
        s8.setPosition(7);
        s8.setDelModel(false);
        s8.setGridView(gvGas);
        s8.setImageList(specialFireBean.getGas_site_standards().getImage());
        singleImageBeans.add(s8);

        mulGridViewBean.setSingleImageList(singleImageBeans);
        refreshAdapter();
    }

    private void doPost() {
        Map<String, String> a1 = new HashMap<>();
        a1.put("is_ok", mFirePerformanceIsOK + "");
        a1.put("problem", etFirePerformanceProblem.getText().toString());
        a1.put("measures", etFirePerformanceMeasure.getText().toString());

        Map<String, String> a2 = new HashMap<>();
        a2.put("is_ok", mCableWellIsOK + "");
        a2.put("problem", etCableWellProblem.getText().toString());
        a2.put("measures", etCableWellMeasure.getText().toString());

        Map<String, String> a3 = new HashMap<>();
        a3.put("is_ok", mFireWallLimitIsOK + "");
        a3.put("problem", etFireWallLimitProblem.getText().toString());
        a3.put("measures", etFireWallLimitMeasure.getText().toString());

        Map<String, String> a4 = new HashMap<>();
        a4.put("is_ok", mFireBlockIsOK + "");
        a4.put("problem", etFireBlockProblem.getText().toString());
        a4.put("measures", etFireBlockMeasure.getText().toString());

        Map<String, String> a5 = new HashMap<>();
        a5.put("is_ok", mTubeWellsUse + "");
        a5.put("problem", etTubeWellsUseProblem.getText().toString());
        a5.put("measures", etTubeWellsUseMeasure.getText().toString());

        Map<String, String> a6 = new HashMap<>();
        a6.put("is_ok", mElectricLineIsOk + "");
        a6.put("problem", etElectricLineProblem.getText().toString());
        a6.put("measures", etElectricLineMeasure.getText().toString());

        Map<String, String> a7 = new HashMap<>();
        a7.put("is_ok", mElectricityLoadIsOk + "");
        a7.put("problem", etElectricityLoadProblem.getText().toString());
        a7.put("measures", etElectricityLoadMeasure.getText().toString());

        Map<String, String> a8 = new HashMap<>();
        a8.put("is_ok", mGasIsOk + "");
        a8.put("problem", etGasProblem.getText().toString());
        a8.put("measures", etGasMeasure.getText().toString());

        Map<String, Map<String, String>> dataParams = new HashMap<>();
        dataParams.put("wall_fireproof_performance", a1);
        dataParams.put("cable_pipe_independent", a2);
        dataParams.put("well_pipe_reach", a3);
        dataParams.put("cable_pipe_sealed", a4);

        dataParams.put("pipe_well_occupation", a5);
        dataParams.put("electrical_wiring_equipment", a6);
        dataParams.put("fire_power_load", a7);
        dataParams.put("gas_site_standards", a8);


        MultipartBody.Builder builder = new MultipartBody.Builder();

        putBody(builder, AppConfig.ACCESS_TOKEN, AppMediaType.TEXT, UserInfoUtil.getCurrentUserAccessToken());
        putBody(builder, "build_id", AppMediaType.TEXT, build_id);
        putBody(builder, "type", AppMediaType.TEXT, 1 + "");
        putBody(builder, "data", AppMediaType.TEXT, new Gson().toJson(dataParams));
        putBody(builder, "action_type", AppMediaType.TEXT, "special_fire_protection");


        // 图片数组
        File file;
        int size = mulGridViewBean.getSingleImageList().size();
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < mulGridViewBean.getSingleImageList().get(i).getImageList().size(); j++) {
                if (mulGridViewBean.getSingleImageList().get(i).getImageList().get(j).contains("http")) {
                    Log.e("ttt     ", mulGridViewBean.getSingleImageList().get(i).getImageList().get(j) + "\n");
                    continue;
                } else {
                    file = new File(mulGridViewBean.getSingleImageList().get(i).getImageList().get(j));
                    if (i == 0) {
                        putBody(builder, "image*wall_fireproof_performance[]", AppMediaType.JPG, file);
                    } else if (i == 1) {
                        putBody(builder, "image*cable_pipe_independent[]", AppMediaType.JPG, file);
                    } else if (i == 2) {
                        putBody(builder, "image*well_pipe_reach[]", AppMediaType.JPG, file);
                    }
                    else if (i == 3) {
                        putBody(builder, "image*cable_pipe_sealed[]", AppMediaType.JPG, file);
                    }
                    else if (i == 4) {
                        putBody(builder, "image*pipe_well_occupation[]", AppMediaType.JPG, file);
                    }
                    else if (i == 5) {
                        putBody(builder, "image*electrical_wiring_equipment[]", AppMediaType.JPG, file);
                    }
                    else if (i == 6) {
                        putBody(builder, "image*fire_power_load[]", AppMediaType.JPG, file);
                    }
                    else if (i == 7) {
                        putBody(builder, "image*gas_site_standards[]", AppMediaType.JPG, file);
                    }
                    else {
                        continue;
                    }
                }
            }
        }

        builder.setType(MultipartBody.FORM);
        MultipartBody multipartBody = builder.build();

        HttpMethods.getInstance().postSpecialFireInfo(multipartBody)
                .subscribe(new SampleProgressObserver<SpecialFireBean>(this) {
                    @Override
                    public void onNext(SpecialFireBean specialFireBean) {
                        ToastUtil.getInstances().showMsg("保存成功");
                    }
                });
    }

    @OnClick({R.id.tvBack, R.id.tvSearch})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvBack:
                finish();
                break;
            case R.id.tvSearch:
                downloadImage();
                break;
            default:
                break;
        }
    }

    @Override
    public void post() {
        doPost();
    }
}
